package com.lic.license.service.impl;


import com.lic.license.core.License;
import com.lic.license.core.LicenseCheck;
import com.lic.license.manager.CustomLicenseManager;
import com.lic.license.param.CustomKeyStoreParam;
import com.lic.license.util.FileUtil;
import com.lic.system.pojo.LicensePram;
import com.lic.system.service.LicenseService;
import com.lic.system.threadLocal.UserThreadLocal;
import de.schlichtherle.license.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.security.auth.x500.X500Principal;
import java.io.File;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.prefs.Preferences;

/**
 * License生成类
 */
@Slf4j
@Component
public class LicenseCreate {


    private LicenseService licenseService;

    private final static X500Principal DEFAULT_HOLDER_AND_ISSUER = new X500Principal("CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN");


    public LicenseCreate(LicenseService licenseService) {
        this.licenseService = licenseService;
    }

    private License param;

    public void setParam(License param) {
        this.param = param;
    }


    /**
     * 生成License证书
     */
    public void generateLicense() throws Exception {
            LicenseManager licenseManager = new CustomLicenseManager(initLicenseParam());
            LicenseContent licenseContent = initLicenseContent();
            //将公司的名称创建文件夹
            String s = "../license/" + param.getLicenseCheck().getCompanyName();
            FileUtil.ofFile(s);
            String path = s + "/license.lic";
            licenseManager.store(licenseContent, new File(path));

    }

    /**
     * 初始化证书生成参数
     */
    private LicenseParam initLicenseParam() {
        Preferences preferences = Preferences.userNodeForPackage(LicenseCreate.class);

        //设置对证书内容加密的秘钥
        CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());

        KeyStoreParam privateStoreParam = new CustomKeyStoreParam(LicenseCreate.class
                , param.getPrivateKeysStorePath()
                , param.getPrivateAlias()
                , param.getStorePass()
                , param.getKeyPass());

        LicenseParam licenseParam = new DefaultLicenseParam(param.getSubject()
                , preferences
                , privateStoreParam
                , cipherParam);

        return licenseParam;
    }

    /**
     * 设置证书生成正文信息
     */
    private LicenseContent initLicenseContent() {
        LicenseContent licenseContent = new LicenseContent();
        licenseContent.setHolder(DEFAULT_HOLDER_AND_ISSUER);
        licenseContent.setIssuer(DEFAULT_HOLDER_AND_ISSUER);

        // 设置证书名称
        licenseContent.setSubject(param.getSubject());
        // 设置证书有效期
        licenseContent.setIssued(param.getIssuedTime());
        // 设置证书生效日期
        licenseContent.setNotBefore(param.getIssuedTime());
        // 设置证书失效日期
        licenseContent.setNotAfter(param.getExpiryTime());
        // 设置证书用户类型
        licenseContent.setConsumerType(param.getConsumerType());
        // 设置证书用户数量
        licenseContent.setConsumerAmount(param.getConsumerAmount());
        // 设置证书描述信息
        licenseContent.setInfo(param.getDescription());

        //扩展校验服务器硬件信息
        licenseContent.setExtra(param.getLicenseCheck());

        return licenseContent;
    }

}
